package com.suning.sawp.service.impl.promotion;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.suning.rsc.dto.responsedto.MbfResponse;
import com.suning.rsc.httpservice.annotation.EsbEIHttpWired;
import com.suning.sawp.constants.ConfigConstants;
import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.constants.LoggerConstants;
import com.suning.sawp.constants.PromSysConstants;
import com.suning.sawp.constants.PromotionConstants;
import com.suning.sawp.constants.PromotionErrorConstants;
import com.suning.sawp.dto.aircondition.CommInfoReq;
import com.suning.sawp.dto.aircondition.CouponReq;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.esb.CouponOperateReq;
import com.suning.sawp.dto.esb.CouponOperateResp;
import com.suning.sawp.dto.esb.CouponRuleQueryReq;
import com.suning.sawp.dto.esb.CouponRuleQueryRtn;
import com.suning.sawp.dto.esb.QryActDetailNewReq;
import com.suning.sawp.dto.esb.QryActDetailNewRsp;
import com.suning.sawp.dto.esb.QryActDetailReq;
import com.suning.sawp.dto.esb.QryActDetailRsp;
import com.suning.sawp.dto.esb.QryCouponListReq;
import com.suning.sawp.dto.esb.QryCouponListReq.ProductDetailReq;
import com.suning.sawp.dto.esb.QryCouponListRsp;
import com.suning.sawp.dto.esb.QueryCouponRtn;
import com.suning.sawp.dto.esb.SendCouponRtn;
import com.suning.sawp.intf.esb.CouponRuleQueryEsbService;
import com.suning.sawp.intf.esb.PromotionCouponEsbService;
import com.suning.sawp.intf.esb.PromotionSysService;
import com.suning.sawp.intf.promotion.CityTransService;
import com.suning.sawp.intf.promotion.PromotionCouponService;
import com.suning.sawp.po.promotion.ValidActivity;
import com.suning.sawp.service.util.ConfigUtil;
import com.suning.sawp.service.util.DateUtils;
import com.suning.sawp.service.util.SCMConfigUtil;
import com.suning.sawp.service.util.XmlUtils;
import com.ximpleware.AutoPilot;
import com.ximpleware.VTDNav;

/**
 * 
 * <br>
 * 〈功能详细描述〉
 * 
 * @author Wuxiao
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service
public class PromotionCouponServiceImpl implements PromotionCouponService {

	private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstants.SAWP_INTEGRATION);

	private PromotionCouponEsbService promotionCouponEsbService;

	private CouponRuleQueryEsbService couponRuleQueryEsbService;

	private PromotionSysService promotionSysService;

	@Autowired
	CityTransService cityTransService;

	private static final String DEFAULT_CITY_ID = "000001000173";

	@Override
	public QueryCouponRtn queryCouponDetail(String activityId, String bonusTrigerId, String custNo, String activitySecretKey, String cityId, ValidActivity activity) {
		Object[] args = { activityId, custNo, activitySecretKey, cityId };
		LOGGER.debug("PromotionCouponService.queryCouponDetail, params: activityId={}, custNo={}, activitySecretKey={}, cityId={}", args);
		if (!checkParam(activityId, custNo, activitySecretKey)) {
			return null;
		}
		QueryCouponRtn ret = new QueryCouponRtn();
		CouponOperateReq req = new CouponOperateReq();
		req.setCaller(PromotionConstants.SYS_CODE_SNSAWP);
		// 奖励事件标识 默认38
		if (StringUtils.isNotBlank(bonusTrigerId)) {
			req.setBonusTrigerId(bonusTrigerId);
		}
		req.setActivityId(activityId);
		req.setActivitySecretKey(activitySecretKey);
		// 城市编码转换 促销中心编码12位 5个0+mdm7位编码
		if (StringUtils.isNotBlank(cityId)) {
			cityId = PromotionConstants.PROMOTION_CITY_PREFIX + cityTransService.getCommerceFromMDMCode(cityId);
		}
		req.setCityId(cityId);
		if (activity != null && activity.getCouponResource() != null && PromotionConstants.PROMOT_COUPON_RESCOUCE_OFFLINE_STORE == activity.getCouponResource()) {
			req.setTerminalId(PromSysConstants.TERMINAL_ID_STORE);
			req.setChannelId(PromSysConstants.SEND_COUPON_CHANNEL_ID_STORE);
			req.setStoreCode(activity.getStoreCode());
			req.setCompanyCode(activity.getBranchCode());
			req.setPayDate(DateUtils.getDateStrByFormat(new Date(), DateUtils.DEFAULT_TIME_PATTERN));
			req.setCityId(StringUtils.isBlank(cityId) ? DEFAULT_CITY_ID : cityId);
		} else if (activity != null && activity.getCouponResource() != null && PromotionConstants.PROMOT_COUPON_RESCOUCE_OFFLINE_MARKET == activity.getCouponResource()) {
			req.setTerminalId(PromSysConstants.TERMINAL_ID_MARKET);
			req.setChannelId(PromSysConstants.SEND_COUPON_CHANNEL_ID_STORE);
			req.setStoreCode(activity.getStoreCode());
			req.setCompanyCode(activity.getBranchCode());
			req.setPayDate(DateUtils.getDateStrByFormat(new Date(), DateUtils.DEFAULT_TIME_PATTERN));
			req.setCityId(StringUtils.isBlank(cityId) ? DEFAULT_CITY_ID : cityId);
		}
		req.setMemberId(custNo);
		req.setOperateType(CouponOperateReq.OperateType.OPERATE_TYPE_QUERY);
		MbfResponse mbfResponse = null;
		try {
			mbfResponse = promotionCouponEsbService.couponOperateService(req);
		} catch (Exception e) {
			LOGGER.error("调用促销中心查询券Id异常,活动Id:" + activityId + "会员编码:" + custNo + "秘钥:" + activitySecretKey + "城市Id" + cityId, e);
		}
		if (mbfResponse != null && mbfResponse.getOutput() != null) {
			CouponOperateResp resp = (CouponOperateResp) mbfResponse.getOutput().getMbfBody(CouponOperateResp.class);
			if (resp != null && PromotionConstants.SUCCESS.equals(resp.getResultFlag())) {
				ret.setResult(resp.getResultFlag());
				ret.setCouponId(resp.getCouponRuleCode());
				ret.setCouponName(resp.getCouponName());
				ret.setCouponNo(resp.getCouponNo());
				String couponType = ConfigUtil.getConfig(ConfigConstants.SAWP_CONFIG_COUPON_TYPE, resp.getCouponType());
				ret.setCouponType(StringUtils.isNotBlank(couponType) ? couponType : PromotionConstants.COUPON_TYPES_MAP.get(resp.getCouponType()));
				ret.setCouponAmount(resp.getCouponAmount());
			} else if (resp != null && PromotionConstants.FAILED.equals(resp.getResultFlag())) {
				ret.setResult(resp.getResultFlag());
				ret.setFailReason(resp.getFailCode());
				ret.setFailMsg(resp.getFailMessage());
				LOGGER.info("调用促销中心查询券Id失败,错误编码:{},错误信息:{}", resp.getFailCode(), resp.getFailMessage());
			}
		}
		return ret;
	}

	private boolean checkParam(String... args) {
		for (String s : args) {
			if (StringUtils.isBlank(s)) {
				return false;
			}
		}
		return true;
	}

	public PromotionCouponEsbService getPromotionCouponEsbService() {
		return promotionCouponEsbService;
	}

	@EsbEIHttpWired
	public void setPromotionCouponEsbService(PromotionCouponEsbService promotionCouponEsbService) {
		this.promotionCouponEsbService = promotionCouponEsbService;
	}

	public CouponRuleQueryEsbService getCouponRuleQueryEsbService() {
		return couponRuleQueryEsbService;
	}

	@EsbEIHttpWired
	public void setCouponRuleQueryEsbService(CouponRuleQueryEsbService couponRuleQueryEsbService) {
		this.couponRuleQueryEsbService = couponRuleQueryEsbService;
	}

	public PromotionSysService getPromotionSysService() {
		return promotionSysService;
	}

	@EsbEIHttpWired
	public void setPromotionSysService(PromotionSysService promotionSysService) {
		this.promotionSysService = promotionSysService;
	}

	@Override
	public SendCouponRtn sendCoupon(String activityId, String cityId, String custNo, String activitySecretKey, String bonusTrigerId, ValidActivity activity, String offLineOrderId) {
		if (!checkParam(activityId, custNo, activitySecretKey)) {
			return null;
		}
		SendCouponRtn ret = new SendCouponRtn();
		CouponOperateReq req = new CouponOperateReq();
		req.setCaller(PromotionConstants.SYS_CODE_SNSAWP);
		req.setBonusTrigerId(bonusTrigerId);
		req.setActivityId(activityId);
		req.setActivitySecretKey(activitySecretKey);
		req.setMemberId(custNo);
		// 城市编码转换 促销中心编码12位 5个0+mdm7位编码
		if (StringUtils.isNotBlank(cityId)) {
			cityId = PromotionConstants.PROMOTION_CITY_PREFIX + cityTransService.getCommerceFromMDMCode(cityId);
		}
		req.setCityId(cityId);
		if (activity != null && activity.getCouponResource() != null && PromotionConstants.PROMOT_COUPON_RESCOUCE_OFFLINE_STORE == activity.getCouponResource()) {
			req.setTerminalId(PromSysConstants.TERMINAL_ID_STORE);
			req.setChannelId(PromSysConstants.SEND_COUPON_CHANNEL_ID_STORE);
			req.setStoreCode(activity.getStoreCode());
			req.setCompanyCode(activity.getBranchCode());
			req.setOrderId(offLineOrderId);
			req.setPayDate(DateUtils.getDateStrByFormat(new Date(), DateUtils.DEFAULT_TIME_PATTERN));
			req.setCityId(StringUtils.isBlank(cityId) ? DEFAULT_CITY_ID : cityId);
		} else if (activity != null && activity.getCouponResource() != null && PromotionConstants.PROMOT_COUPON_RESCOUCE_OFFLINE_MARKET == activity.getCouponResource()) {
			req.setTerminalId(PromSysConstants.TERMINAL_ID_MARKET);
			req.setChannelId(PromSysConstants.SEND_COUPON_CHANNEL_ID_STORE);
			req.setStoreCode(activity.getStoreCode());
			req.setCompanyCode(activity.getBranchCode());
			req.setOrderId(offLineOrderId);
			req.setPayDate(DateUtils.getDateStrByFormat(new Date(), DateUtils.DEFAULT_TIME_PATTERN));
			req.setCityId(StringUtils.isBlank(cityId) ? DEFAULT_CITY_ID : cityId);
		}
		req.setOperateType(CouponOperateReq.OperateType.OPERATE_TYPE_SEND);
		MbfResponse mbfResponse = null;
		try {
			mbfResponse = promotionCouponEsbService.couponOperateService(req);
			LOGGER.debug("调用促销中心发券接口响应报文:{}", mbfResponse.toString());
		} catch (Exception e) {
			LOGGER.error("调用促销中心发券接口异常,活动Id:" + activityId + "会员编码:" + custNo + "秘钥:" + activitySecretKey, e);
		}
		if (mbfResponse != null && mbfResponse.getOutput() != null) {
			CouponOperateResp resp = (CouponOperateResp) mbfResponse.getOutput().getMbfBody(CouponOperateResp.class);
			if (resp != null) {
				ret.setRetFlag(resp.getResultFlag());
				ret.setErrorCode(resp.getFailCode());
				ret.setErrorMsg(resp.getFailMessage());
				if (PromotionConstants.SUCCESS.equals(resp.getResultFlag())) {
					ret.setCouponId(resp.getCouponRuleCode());
					ret.setCouponName(resp.getCouponName());
					ret.setCouponNo(resp.getCouponNo());
					String couponType = ConfigUtil.getConfig(ConfigConstants.SAWP_CONFIG_COUPON_TYPE, resp.getCouponType());
					ret.setCouponType(StringUtils.isNotBlank(couponType) ? couponType : PromotionConstants.COUPON_TYPES_MAP.get(resp.getCouponType()));
					ret.setCouponAmount(resp.getCouponAmount());
					ret.setCouponStartTime(resp.getCouponStartTime());
					ret.setCouponEndTime(resp.getCouponEndTime());
				}
			}
		}
		return ret;
	}

	@Override
	public CouponRuleQueryRtn queryCouponRuleDetail(String ruleId) {
		if (!checkParam(ruleId)) {
			return null;
		}
		CouponRuleQueryReq req = new CouponRuleQueryReq();
		req.setCouponRulesId(ruleId);
		req.setSourceSystem(PromotionConstants.SYS_CODE_SNSAWP);
		req.setPartScopeQuantityLimitNumber(ConfigUtil.getConfig(ConfigConstants.SAWP_CONFIG_PATH, ConfigConstants.PRO_COUPON_RULE_QUERY_LIMIT, ConfigConstants.DEFAULT_COUPON_RULE_LIMIT));
		MbfResponse resp = null;
		try {
			resp = couponRuleQueryEsbService.queryCouponRule(req);
			LOGGER.debug("促销中心查询券规则详情响应报文{}", resp.toString());
		} catch (Exception e) {
			LOGGER.error("促销中心查询券规则详情接口异常,券Id:" + ruleId + "", e);
		}
		if (resp != null && resp.getOutput() != null) {
			if ("1".equals(SCMConfigUtil.getConfig(ConfigConstants.XML_VTD_DEAL_FLG))) {
				return getCouponRuleQueryRtnVtd(resp.toString(), ruleId);
			} else {
				if (resp.toString().length() <= 153600) {
					return getCouponRuleQueryRtn(resp.toString(), ruleId);
				}
			}
		}
		return null;
	}

	private CouponRuleQueryRtn getCouponRuleQueryRtn(String xmlStr, String ruleId) {
		Document doc;
		try {
			CouponRuleQueryRtn ret = new CouponRuleQueryRtn();
			doc = XmlUtils.getW3CDom(xmlStr);
			String retFlag = XmlUtils.getValueByXpath(doc, "/MbfService/output1/queryResult");
			String errorMsg = XmlUtils.getValueByXpath(doc, "/MbfService/output1/failureReasons");
			if (PromotionConstants.SUCCESS.equals(retFlag)) {
				String couponRulesName = XmlUtils.getValueByXpath(doc, "/MbfService/output1/couponRulesName");
				String couponRulesShowMsg = XmlUtils.getValueByXpath(doc, "/MbfService/output1/couponRulesShowMsg");
				String rewardsPreferential = XmlUtils.getValueByXpath(doc, "/MbfService/output1/rewardsPreferential");
				NodeList nodeList = XmlUtils.runXpath(doc, "/MbfService/output1/storesList/storesDetail", null);
				List<String> storeNameList = new ArrayList<String>();
				int length = nodeList.getLength();
				for (int i = 0; i < length; i++) {
					Node node = nodeList.item(i);
					storeNameList.add(XmlUtils.getValueByXpath(node, "storesName"));
				}
				List<String> noUseList = new ArrayList<String>();
				NodeList noMarketList = XmlUtils.runXpath(doc, "/MbfService/output1/noMarketNameList/noMarketNameDetail", null);
				int noMarketLength = noMarketList.getLength();
				for (int i = 0; i < noMarketLength; i++) {
					Node node = noMarketList.item(i);
					noUseList.add(XmlUtils.getValueByXpath(node, "noMarketName"));
				}
				List<String> terminalList = new ArrayList<String>();
				NodeList couponRulesTerminalList = XmlUtils.runXpath(doc, "/MbfService/output1/couponRulesTerminalList/couponRulesTerminalDetail", null);
				int terminalLength = couponRulesTerminalList.getLength();
				for (int i = 0; i < terminalLength; i++) {
					Node node = couponRulesTerminalList.item(i);
					terminalList.add(XmlUtils.getValueByXpath(node, "couponRulesTerminalName"));
				}
				ret.setRetFlag(retFlag);
				ret.setErrorMsg(errorMsg);
				ret.setCouponRulesName(couponRulesName);
				ret.setCouponRulesShowMsg(couponRulesShowMsg);
				ret.setRewardsPreferential(rewardsPreferential);
				ret.setStoreNameList(storeNameList);
				ret.setTerminalList(terminalList);
				ret.setNoUseList(noUseList);
			}
			return ret;
		} catch (Exception e) {
			LOGGER.error("促销中心查询券规则详情解析报文失败 getValueByXpath,券Id:" + ruleId + "", e);
		}

		return null;
	}

	private CouponRuleQueryRtn getCouponRuleQueryRtnVtd(String xmlStr, String ruleId) {
		try {
			CouponRuleQueryRtn ret = new CouponRuleQueryRtn();
			VTDNav vn = XmlUtils.getVTDNav(xmlStr);
			AutoPilot ap = new AutoPilot(vn);
			AutoPilot ape = new AutoPilot(vn);
			ape.selectXPath("/MbfService/output1/queryResult");
			String retFlag = ape.evalXPathToString();
			ape.selectXPath("/MbfService/output1/couponRulesName");
			String errorMsg = ape.evalXPathToString();
			if (PromotionConstants.SUCCESS.equals(retFlag)) {
				ape.selectXPath("/MbfService/output1/couponRulesName");
				String couponRulesName = ape.evalXPathToString();
				ape.selectXPath("/MbfService/output1/couponRulesShowMsg");
				String couponRulesShowMsg = ape.evalXPathToString();
				ape.selectXPath("/MbfService/output1/rewardsPreferential");
				String rewardsPreferential = ape.evalXPathToString();
				ape.selectXPath("/MbfService/output1/storesList/storesDetail");
				List<String> storeNameList = new ArrayList<String>();
				String storeName = "";
				while (ape.evalXPath() != -1) {
					ap.selectXPath("storesName");
					storeName = ap.evalXPathToString();
					storeNameList.add(storeName);
				}
				List<String> noUseList = new ArrayList<String>();
				ape.selectXPath("/MbfService/output1/noMarketNameList/noMarketNameDetail");
				while (ape.evalXPath() != -1) {
					ap.selectXPath("noMarketName");
					noUseList.add(ap.evalXPathToString());
				}
				List<String> terminalList = new ArrayList<String>();
				ape.selectXPath("/MbfService/output1/couponRulesTerminalList/couponRulesTerminalDetail");
				while (ape.evalXPath() != -1) {
					ap.selectXPath("couponRulesTerminalName");
					noUseList.add(ap.evalXPathToString());
				}
				ret.setRetFlag(retFlag);
				ret.setErrorMsg(errorMsg);
				ret.setCouponRulesName(couponRulesName);
				ret.setCouponRulesShowMsg(couponRulesShowMsg);
				ret.setRewardsPreferential(rewardsPreferential);
				ret.setStoreNameList(storeNameList);
				ret.setTerminalList(terminalList);
				ret.setNoUseList(noUseList);
				return ret;
			}
		} catch (Exception e) {
			LOGGER.error("促销中心查询券规则详情解析报文失败 VTD,券Id:" + ruleId + "", e);
		}
		return null;
	}

	@Override
	public QryActDetailRsp queryProActivityList(String cityId, String bonusTrigerId, Integer couponRescource, String storeCode) {
		QryActDetailReq req = new QryActDetailReq();
		// 城市编码转换 促销中心编码12位 5个0+mdm7位编码
		if (StringUtils.isNotBlank(cityId) && cityId.length() < 12) {
			cityId = PromotionConstants.PROMOTION_CITY_PREFIX + cityTransService.getCommerceFromMDMCode(cityId);
		}
		// 默认传38，如果有值则替换
		if (StringUtils.isNotBlank(bonusTrigerId)) {
			req.setBonusTrigerId(bonusTrigerId);
		}
		req.setCityId(cityId);
		if (couponRescource == PromotionConstants.PROMOT_COUPON_RESCOUCE_OFFLINE_STORE) {
			req.setActivityInitiator(PromSysConstants.ACTIVITY_INITIATOR_PLAT_OFFLINE);
			req.setChannelId(PromSysConstants.CHANNEL_ID_STORE);
			req.setEntityStoreID(storeCode);
		} else if (couponRescource == PromotionConstants.PROMOT_COUPON_RESCOUCE_OFFLINE_MARKET) {
			req.setActivityInitiator(PromSysConstants.ACTIVITY_INITIATOR_PLAT_OFFLINE);
			req.setChannelId(PromSysConstants.CHANNEL_ID_MARKET);
			req.setEntityStoreID(storeCode);
		} else if (couponRescource == PromotionConstants.PROMOT_COUPON_RESCOUCE_EXCHANGE) {
			req.setChannelId(PromSysConstants.CHANNEL_ID_WEBSITE);
		}
		MbfResponse response = null;
		QryActDetailRsp ret = new QryActDetailRsp();
		try {
			response = promotionSysService.queryActivityDetail(req);
			LOGGER.debug("调用促销中心查询活动列表响应报文{}", response.toString());
		} catch (Exception e) {
			ret.setRequestResult(ReturnMsg.FAIL);
			ret.setErrorCode(ErrorCodeConstants.ERR_0002);
			ret.setFailedReson(ErrorCodeConstants.ERR_MAP.get(ErrorCodeConstants.ERR_0002));
			LOGGER.error("调用促销中心查询活动列表接口异常,cityId=" + cityId, e);
		}
		if (response != null && response.getOutput() != null) {
			ret = (QryActDetailRsp) response.getOutput().getMbfBody(QryActDetailRsp.class);
			if (PromotionConstants.FAILED.equals(ret.getRequestResult())) {
				ret.setErrorCode(PromotionErrorConstants.ACT_QUERY_ERR_E002);
			}
		}
		return ret;
	}

	@Override
	public QryActDetailRsp queryProActivityList(String cityId, Integer couponResource, String storeCode) {
		return queryProActivityList(cityId, PromSysConstants.BONUS_TRIGERID_STORE, couponResource, storeCode);
	}

	@Override
	public QryActDetailNewRsp queryProActivityListNew(String cityId, Integer couponResource, String storeCode) {
		return queryProActivityListNew(cityId, PromSysConstants.BONUS_TRIGERID_STORE, couponResource, storeCode);
	}

	@Override
	public QryActDetailNewRsp queryProActivityListNew(String cityId, String bonusTrigerId, Integer couponRescource, String storeCode) {
		QryActDetailNewReq req = new QryActDetailNewReq();
		// 城市编码转换 促销中心编码12位 5个0+mdm7位编码
		if (StringUtils.isNotBlank(cityId)) {
			cityId = PromotionConstants.PROMOTION_CITY_PREFIX + cityTransService.getCommerceFromMDMCode(cityId);
		}
		// 默认传38，如果有值则替换
		if (StringUtils.isNotBlank(bonusTrigerId)) {
			req.setBonusTriggerId(bonusTrigerId);
		}
		req.setCityId(cityId);
		if (couponRescource == PromotionConstants.PROMOT_COUPON_RESCOUCE_OFFLINE_STORE) {
			req.setActivityInitiator(PromSysConstants.ACTIVITY_INITIATOR_PLAT_OFFLINE);
			req.setChannelId(PromSysConstants.CHANNEL_ID_STORE);
			req.setStoreCode(storeCode);
		} else if (couponRescource == PromotionConstants.PROMOT_COUPON_RESCOUCE_OFFLINE_MARKET) {
			req.setActivityInitiator(PromSysConstants.ACTIVITY_INITIATOR_PLAT_OFFLINE);
			req.setChannelId(PromSysConstants.CHANNEL_ID_MARKET);
			req.setStoreCode(storeCode);
		}
		MbfResponse response = null;
		QryActDetailNewRsp ret = new QryActDetailNewRsp();
		try {
			LOGGER.info("queryProActivityListNew 调用促销中心查询活动列表响应报文req:{}", req.toString());
			response = promotionSysService.searchAvailableCouponAct(req);
			LOGGER.info("queryProActivityListNew 调用促销中心查询活动列表响应报文{}", response.toString());
		} catch (Exception e) {
			ret.setRequestResult(ReturnMsg.FAIL);
			ret.setErrorCode(ErrorCodeConstants.ERR_0002);
			LOGGER.error(" queryProActivityListNew 调用促销中心查询活动列表接口异常,cityId=" + cityId, e);
			ret.setFailedReson(ErrorCodeConstants.ERR_MAP.get(ErrorCodeConstants.ERR_0002));
		}
		if (response != null && response.getOutput() != null) {
			ret = (QryActDetailNewRsp) response.getOutput().getMbfBody(QryActDetailNewRsp.class);
			if (PromotionConstants.FAILED.equals(ret.getRequestResult())) {
				ret.setErrorCode(PromotionErrorConstants.ACT_QUERY_ERR_E002);
			} else {
				ret.setRequestResult(PromotionConstants.SUCCESS);
			}
		}
		return ret;
	}

	@Override
	public QueryCouponRtn queryCouponDetail(String activityId, String custNo, String activitySecretKey, String cityId, ValidActivity activity) {
		return queryCouponDetail(activityId, PromotionConstants.SYS_CODE_SNSAWP, custNo, activitySecretKey, cityId, activity);
	}

	@Override
	public QryCouponListRsp queryCouponByCommList(CouponReq couponReq) {
		QryCouponListReq request = new QryCouponListReq();
		QryCouponListRsp ret = new QryCouponListRsp();
		MbfResponse response = null;
		try {
			request.setCityId(couponReq.getCityId());
			List<ProductDetailReq> commList = new ArrayList<ProductDetailReq>();
			if (CollectionUtils.isNotEmpty(couponReq.getCommInfoList())) {
				for (CommInfoReq item : couponReq.getCommInfoList()) {
					ProductDetailReq detail = new ProductDetailReq();
					detail.setMarketingActivityType(item.getActType());
					detail.setProductId(item.getCommCode());
					detail.setStoreId(item.getStoreId());
					detail.setBusinessSign("");
					commList.add(detail);
				}
			}
			request.setProductList(commList);
			LOGGER.info("queryCouponByCommList request:{}", request.toString());
			response = promotionSysService.batchQueryFreeCouponAct(request);
			LOGGER.info("queryCouponByCommList response {}", response.toString());
		} catch (Exception e) {
			ret.setResult(ReturnMsg.FAIL);
			ret.setErrorCode(ErrorCodeConstants.ERR_0002);
			LOGGER.error(" queryCouponByCommList ,couponReq=" + couponReq, e);
			ret.setFailedReson(ErrorCodeConstants.ERR_MAP.get(ErrorCodeConstants.ERR_0002));
		}

		if (response != null && response.getOutput() != null) {
			ret = (QryCouponListRsp) response.getOutput().getMbfBody(QryCouponListRsp.class);
			if (PromotionConstants.FAILED.equals(ret.getResult())) {
				ret.setErrorCode(PromotionErrorConstants.ACT_QUERY_ERR_E002);
			}
		}
		return ret;
	}

}
